package com.third.tencentlogin.controller;


import com.third.tencentlogin.model.OAuthUser;
import com.third.tencentlogin.model.User;
import com.third.tencentlogin.oauth.service.OAuthServiceDeractor;
import com.third.tencentlogin.oauth.service.OAuthServices;
import com.third.tencentlogin.oauth.service.QQOAuthServices;
import com.third.tencentlogin.repository.OauthUserRepository;
import com.third.tencentlogin.repository.UserRepository;
import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AccountController {
    public static final Logger log = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    OAuthServices oAuthServices;
    @Autowired
    OauthUserRepository oauthUserRepository;
    @Autowired
    UserRepository userRepository;

    /**
     * 登录路径
     * @param model
     * @return
     */
    @RequestMapping(value = {"","/login"}, method = RequestMethod.GET)
    public String showLogin(Model model){
        model.addAttribute("oAuthServices", oAuthServices.getAllOAuthServices());
        return "index";
    }


    /**
     * 回调路径
     * @param code
     * @param type
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/oauth/{type}/callback", method = RequestMethod.GET)
    public String calaback(@RequestParam(value = "code",  required = true) String code, @PathVariable(value = "type") String type, HttpServletRequest request, Model model){
        OAuthServiceDeractor oAuthServiceDeractor = oAuthServices.getOAuthService(type);
        Token accessToken = oAuthServiceDeractor.getAccessToken(null, new Verifier(code));
        OAuthUser oAuthInfo = oAuthServiceDeractor.getOAuthUser(accessToken);
        OAuthUser oAuthUser = oauthUserRepository.findByOAuthTypeAndOauthId(oAuthInfo.getoAuthType(), oAuthInfo.getOauthId());
        if (null == oAuthUser){
            model.addAttribute("oAuthInfo", oAuthInfo);
            return "register";
        }
        request.getSession().setAttribute("oauthUser",oAuthUser);
        return "redirect:/success";
    }


    /**
     * 注册
     * @param model
     * @param user
     * @param oAuthType
     * @param oAuthId
     * @param request
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(Model model, User user,
                           @RequestParam(value = "oAuthType", required = false, defaultValue = "") String oAuthType,
                           @RequestParam(value = "oAuthId", required = true, defaultValue = "") String oAuthId,
                           HttpServletRequest request){
        OAuthUser oAuthInfo = new OAuthUser();
        oAuthInfo.setOauthId(oAuthId);
        oAuthInfo.setoAuthType(oAuthType);
        if (userRepository.findByUsername(user.getUsername()) != null){
            model.addAttribute("errorMessage","用户名已存在");
            model.addAttribute("oAuthInfo",oAuthInfo);
            return "register";
        }
        user = userRepository.save(user);
        OAuthUser oAuthUser = oauthUserRepository.findByOAuthTypeAndOauthId(oAuthType, oAuthId);
        if (oAuthUser == null){
            oAuthInfo.setUser(user);
            oAuthUser = oauthUserRepository.save(oAuthInfo);
        }
        request.getSession().setAttribute("oauthUser", oAuthUser);
        return "redirect:/success";
    }


    /**
     * 登录成功
     * @param request
     * @return
     */
    @RequestMapping(value = "/success", method = RequestMethod.GET)
    @ResponseBody
    public Object success(HttpServletRequest request){
        return request.getSession().getAttribute("oauthUser");
    }
}
